What is valid-url?
The valid-url npm package provides utilities for URL validation and parsing. It allows developers to check if a given string is a valid URL and to parse URLs into their components.
What are valid-url's main functionalities?
URL Validation
This feature allows you to check if a string is a valid URL. The `isUri` method returns the URL if it's valid, otherwise undefined. It's useful for validating URLs before attempting to make HTTP requests or storing them in a database.
"use strict";\nconst validUrl = require('valid-url');\n\nif (validUrl.isUri('http://www.google.com')){\n console.log('Looks like an URI');\n} else {\n console.log('Not a URI');\n}"
URL Parsing
While the primary focus of valid-url is validation, the returned valid URI string from `isUri` can be further parsed using other libraries or custom code to extract components like protocol, host, path, and query parameters. This example demonstrates basic usage for validation, hinting at further parsing possibilities.
"use strict";\nconst validUrl = require('valid-url');\n\nlet uri = validUrl.isUri('http://www.example.com/path?name=query');\nif (uri){\n console.log('URI is', uri);\n // Additional parsing logic here\n} else {\n console.log('Not a URI');\n}"
Other packages similar to valid-url
validator
Validator is a library that offers a wide range of string validation and sanitization methods, including URL validation. Compared to valid-url, validator provides a broader set of functionalities beyond URLs, making it suitable for more comprehensive input validation needs.
url-regex
url-regex is a package specifically designed to provide regular expressions for URL validation. Unlike valid-url, which offers simple validation checks, url-regex allows for more customizable and potentially stricter URL validation based on regex patterns.
URI validation functions
Synopsis
Common url validation methods
var validUrl = require('valid-url');
if (validUrl.isUri(suspect)){
console.log('Looks like an URI');
} else {
console.log('Not a URI');
}
Replicates the functionality of Richard Sonnen sonnen@richardsonnen.com perl module :
http://search.cpan.org/~sonnen/Data-Validate-URI-0.01/lib/Data/Validate/URI.pm full code here
into a nodejs module. Translated practically line by line from perl.
It passes all the original tests.
Description
(copied from original perl module)
This module collects common URI validation routines to make input validation, and untainting easier and more readable.
All functions return an untainted value if the test passes, and undef if it fails. This means that you should always check for a defined status explicitly. Don't assume the return will be true.
The value to test is always the first (and often only) argument.
There are a number of other URI validation modules out there as well (see below.) This one focuses on being fast, lightweight, and relatively 'real-world'. i.e. it's good if you want to check user input, and don't need to parse out the URI/URL into chunks.
Right now the module focuses on HTTP URIs, since they're arguably the most common. If you have a specialized scheme you'd like to have supported, let me know.
Installation
npm install valid-url
Methods
See also
RFC 3986, RFC 3966, RFC 4694, RFC 4759, RFC 4904